{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\" 获取PDF表格中的数据 \"\"\"\n",
    "# 导入必要库\n",
    "import pdfplumber\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 打开pdf文件\n",
    "pdf = pdfplumber.open(R'data\\高考核心词汇1278.pdf')\n",
    "first_page = pdf.pages[1]\n",
    "table1 = first_page.extract_table()\n",
    "second_page = pdf.pages[2]\n",
    "table2 = second_page.extract_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "92"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(pdf.pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "table = []\n",
    "for i in range(len(pdf.pages)):\n",
    "    if(i+2)<92:\n",
    "      page = pdf.pages[i + 2]\n",
    "      table.extend(page.extract_table())\n",
    "\n",
    "print(table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "table1_df = pd.DataFrame(table)\n",
    "table1_df.to_csv(R\"data/words_1278.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['', '北大军哥英语核心高频 1278 词', None, None, None, None, None, '']\n",
      "\n",
      "北大军哥英语核心高频 1278 词\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in table1:\n",
    "  print(i)\n",
    "  for j in i:\n",
    "    print(j)\n",
    "  break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for i in range(3):\n",
    "  print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "stu = {\n",
    "  \"学号\": \"P20210202227\",\n",
    "  \"姓名\": \"小丽\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号 P20210202227\n",
      "姓名 小丽\n"
     ]
    }
   ],
   "source": [
    "for key,value in stu.items():\n",
    "  print(key,value)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "9650cb4e16cdd4a8e8e2d128bf38d875813998db22a3c986335f89e0cb4d7bb2"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
